#include "GL/glew.h"
#include "LoadShader.h"
#include "utility.h"
#include "vmath.h"

GLuint program, vao, vbo;

GLfloat vert[] = { 0.6f, 0.9f,  -0.9f, -0.9f,   0.9f, -0.9f};

bool init()
{
	ShaderInfo si[] = { { GL_VERTEX_SHADER, "Shader.vert" },{ GL_FRAGMENT_SHADER, "Shader.frag" },{ GL_NONE, nullptr } };
	program = LoadShade(si);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
	glEnableVertexAttribArray(0);

	glClearColor(0.0f, 0.05f, 0.05f, 1.0f);
	glUseProgram(program);
	return true;
}

void Display(double time)
{
	glClear(GL_COLOR_BUFFER_BIT);

	Matrix4f m = Matrix4f::createRotationAroundAxis(0.0f, 0.0f, (float)(90*time));
	m = m * Matrix4f::createScale(0.5, 0.5, 0.5);
	glUniformMatrix4fv(1, 1, GL_FALSE, (float*)m);

	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void onClose()
{
	glDeleteVertexArrays(1, &vao);
	glDeleteBuffers(1, &vbo);
}

